package leetcode;

import util.TreeNode;

/**
 * @Project : algorithm-learning-java
 * @Package : leetcode
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/1/24 16:11
 */
public class Q0129 {
    int res = 0;

    private void dfs(TreeNode root, int sum) {
        int tmp = sum * 10 + root.val;
        if (root.left == null && root.right == null) {
            res += tmp;
            return;
        }
        if (root.left != null) dfs(root.left, tmp);
        if (root.right != null) dfs(root.right, tmp);
    }

    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        dfs(root, 0);
        return res;
    }
}
